﻿using FlashPay.Util;
using System;

namespace FlashPay.Management.Filters.impl
{
    public class UserDataKeepManageImplUseCache<T> : UserDataKeepManage<T>
    {
        public T data { set; get; }

        private MemoryCacheUtil memoryCacheUtil;

        public UserDataKeepManageImplUseCache(MemoryCacheUtil memoryCacheUtil)
        {
            this.memoryCacheUtil = memoryCacheUtil;
        }

        public Boolean UserDataDecrypt(String token)
        {
            T data = memoryCacheUtil.Get<T>(EncryptHelper.deCryptDES(token));
            if (data != null)
            {
                this.data = data;
                return true;
            }

            return false;
        }

        public String UserDataEncrypt()
        {
            String token = Guid.NewGuid().ToString("N");
            Boolean exists = memoryCacheUtil.Set(token, data, 120);
            return exists? EncryptHelper.enCryptDES(token) : null;
        }
    }
}
